package com.pams.service;


import com.pams.context.BaseContext;
import com.pams.handler.BaseException;
import com.pams.mapper.NoteMapper;

import com.pams.pojo.entity.Note;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoteService {
    @Autowired
    private NoteMapper noteMapper;
    public Integer add(Note note) {
        String currentUsername = BaseContext.getCurrentUsername();
        if(note.getTitle() == null || note.getTitle() == ""){
            throw new BaseException("标题不能为空");
        }
        Note byId = noteMapper.getById(note.getId());
        if(byId == null ){
            note.setUsername(currentUsername);
            noteMapper.insert(note);
            return  note.getId();
        }
        note.setUsername(currentUsername);
        noteMapper.update(note);
        return  note.getId();
    }

    public List<Note> getlist() {
        String currentUsername = BaseContext.getCurrentUsername();
        List<Note> list = noteMapper.getlist(currentUsername);
        return list;
    }

    public Note getSelectedNote(int id) {
        return noteMapper.getById(id);
    }

    public void delete(int id) {
        noteMapper.delete(id);
    }

    public List<Note> search(String title) {
        String currentUsername = BaseContext.getCurrentUsername();
        List<Note> list = noteMapper.search(currentUsername,title);
        return list;
    }
}
